/*
 * OUnit - an OPAQUE compliant framework for Computer Aided Testing
 *
 * Copyright (C) 2010, 2011  Antti Andreimann
 *
 * This file is part of OUnit.
 *
 * OUnit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OUnit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OUnit.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.googlecode.ounit.opaque;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;

import static com.googlecode.ounit.test.moodle.MoodleParams.log;

/**
 * Page object that models questions generated by
 * {@link MockOpaqueService}
 * @author anttix
 *
 */
public class MockQuestionPage {
	@SuppressWarnings("unused")
	private WebDriver driver;
	
	/* FIXME: This will validate only the first quiz on page.
	 * To do better we need to find a way to fetch all %%IDPREFIX%%-s from page.
	 * Moodle does not currently support multiple Opaque questions on
	 * one page so it's not much of an issue (yet)!
	 */
	@FindBy(css = ".quizdiv")
	private WebElement quizDiv;
	@FindBy(css = ".headdiv")
	private WebElement headDiv;
	@FindBy(css = ".cssdiv")
	private WebElement cssDiv;
	@FindBy(css = ".jsdiv")
	private WebElement jsDiv;
	@FindBy(css = ".picdiv")
	private WebElement picDiv;
	@FindBy(css = ".lastreply")
	private WebElement lastReplyText;
	
	@FindBys(value = {
		@FindBy(css = ".quizdiv"),
		@FindBy(xpath = "//input[@type = 'text' and contains(@name, 'nr')]")
	})
	private WebElement answerBox;
	
	@FindBys(value = {
			@FindBy(css = ".quizdiv"),
			@FindBy(xpath = "//input[@type = 'submit' and contains(@name, 'go')]")
	})
	private WebElement answerButton;

	public MockQuestionPage(WebDriver driver) {
		this.driver = driver;
	}
	
	/**
	 * Validate that question page is rendered correctly.
	 * 
	 */
	public void validate() {
		// Sleep for 1 second to give JavaScript on page some time to do it's job
		if (picDiv.isDisplayed())
			try {
				Thread.sleep(1100);
			} catch (InterruptedException e) { }

		assert quizDiv.isDisplayed()  : "Main question DIV is not visible";
		// Moodle 2.1 does not support this undocumented feature any more
		// assert !headDiv.isDisplayed() : "Header contributions did not work";
		assert !cssDiv.isDisplayed()  : "Question CSS was not loaded";
		assert !jsDiv.isDisplayed()   : "Javascript file was not loaded from resources";
		assert !picDiv.isDisplayed()  : "Image file was not loaded from resources";
	}

	public void answer(int i) {
		log("Submitting " + i + " as an answer");
		answerBox.clear();
		answerBox.sendKeys(i + "");
		answerButton.click();
	}

	public String getLastAnswer() {
		return lastReplyText.getText();	
	}
}
